/*
 * GamePanel.java
 *
 * Created on May 7, 2007, 2:30 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package fmglemmings;

import java.awt.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.swing.*;

/**
 *
 * @author ijanos
 */
public class GamePanel extends JPanel{
	private Graphics dbg;
	private Image dbImage = null;
	
	private static final int PHEIGHT=500;
	private static final int PWIDTH=750;
	private static final int CELLH=25;
	private static final int CELLW=25;
	private GameField gf;
	private int mapmaxx,mapmaxy;
	private Map rajzolok;
	
	/** Creates a new instance of GamePanel */
	public GamePanel(GameField agf) {
		gf = agf;
		setBackground(Color.WHITE);
		setPreferredSize(new Dimension(PWIDTH,PHEIGHT));
		fillRajzolok();
	}
	public void setGF(GameField agf){
		gf=agf;
	}
	
	private void fillRajzolok(){
		rajzolok = new HashMap();
		rajzolok.put(Lemming.class,new LemmingView());
		rajzolok.put(DeadLemming.class,new DeadLemmingView());
		rajzolok.put(Spawn.class,new SpawnView());
		rajzolok.put(Goal.class,new GoalView());
		rajzolok.put(BombLemming.class,new BombLemmingView());
		rajzolok.put(DiggerLemming.class,new DiggerLemmingView());
		rajzolok.put(FlyingLemming.class,new FlyingLemmingView());
		rajzolok.put(BlockerLemming.class,new BlockerLemmingView());
		rajzolok.put(BuilderLemming.class,new BuilderLemmingView());
		rajzolok.put(Stone.class,new StoneView());
		rajzolok.put(Bridge.class,new BridgeView());
		rajzolok.put(LeftLadderStone.class,new LeftLadderStoneView());
		rajzolok.put(RightLadderStone.class,new RightLadderStoneView());
	}
	public void gamerender(){
		if (dbImage == null){
			dbImage = createImage(PWIDTH,PHEIGHT);
			if(dbImage == null){
				System.out.println("dbImage is null :(");
				return;
			} else dbg = dbImage.getGraphics();
		}
		dbg.setColor(Color.WHITE);
		dbg.fillRect(0,0,PWIDTH,PHEIGHT);
		try{
			for (Iterator it = gf.mobjects.iterator(); it.hasNext();) {
				MapObject elem = (MapObject)it.next();
				ViewObject rajzol=(ViewObject)rajzolok.get(elem.getClass());
				Graphics g2 = dbg.create();
				g2.translate(elem.getX()*CELLW,elem.getY()*CELLH);
				rajzol.draw(g2,elem);
			}
		} catch (Exception e) {System.out.println(e.toString());}
	}
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		if(dbImage != null){
			g.drawImage(dbImage,0,0,null);
		}
	}
}
